package com.tsing.cli.common.core.xss;

import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;
import org.springframework.util.StringUtils;

import java.util.regex.Pattern;

/**
 * XSS 校验注解实现
 *
 * @author TheTsing
 */
public class XssValidator implements ConstraintValidator<Xss, String> {

    private static final Pattern XSS_PATTERN = Pattern.compile("<(\\S*?)[^>]*>.*?|<.*? />");

    @Override
    public boolean isValid(String value, ConstraintValidatorContext constraintValidatorContext) {
        return !StringUtils.hasText(value) || !XSS_PATTERN.matcher(value).find();
    }

}
